package run.siyuan.service;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import run.siyuan.mapper.CommissionRecordMapper;
import run.siyuan.model.CommissionRecord;
import run.siyuan.model.SimpleEntity;

import java.util.List;

/**
 * @Author: wnJie
 * @Description:
 * @Date: Created in 11:08 2019/8/6 0006
 */
@Slf4j
@Service
public class CommissionRecordService extends ServiceImpl<CommissionRecordMapper, CommissionRecord> {

    private Integer pageSize = 200;

    /**
     * 更新记录
     *
     * @param commissionBillingIds
     */
    public void updateCommissionRecords(List<Long> commissionBillingIds) {
        long current = DateUtil.current();
        for (Long commissionBillingId : commissionBillingIds) {
            int pageNo = 1;
            Page<Long> page = new Page<>(pageNo, pageSize);
            Page<Long> commissionRecordIdPage = this.pageByQuery(page, commissionBillingId);
            Long count = commissionRecordIdPage.getTotal();
            Long totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            while (pageNo <= totalPage) {
                Page<Long> updateCommissionRecordPage = this.pageByQuery(page, commissionBillingId);
                this.update(
                        Wrappers.lambdaUpdate(CommissionRecord.class)
                                .set(CommissionRecord::getEntryBalance, true)
                                .set(CommissionRecord::getEntryBalanceTime, current)
                                .in(SimpleEntity::getId, updateCommissionRecordPage.getRecords())
                );
                pageNo++;
            }
        }
    }

    public Page<Long> pageByQuery(Page<Long> page, Long commissionBillingId) {
        return baseMapper.pageByQuery(page, commissionBillingId);
    }

}
